//    Copyright (C) Kherty.  All rights reserved.
using System.Collections.Generic;
using OpenLS.Core.Serialization;

namespace OpenLS.Spreadsheet
{
    public class TableColumn: IOfficeXmlSerializable
    {
        public TableColumn ()
	{

	}
        public TableColumn(int id, string name)
        {
            this.Id = id;
            this.Name = name;
        }
        #region IOfficeXmlSerializable Members

        void IOfficeXmlSerializable.ReadXml(ReadContext context)
        {
            using (ReadContext c = context.Read("tableColumn"))
            {
                Id = c.GetUIntegerAsInteger("id");
                UniqueName = c.GetOptionalString("uniqueName");
                Name = c.GetString("name");
                TotalsRowFunction = c.GetOptionalEnum<TotalsRowFunction>("totalsRowFunction", TotalsRowFunction.None);
                TotalsRowLabel = c.GetOptionalString("totalsRowLabel");
                queryTableFieldId = c.GetOptionalUIntegerAsInteger("queryTableFieldId");
                StyleSheet ss = c.GetData<Workbook>().StyleSheet ;
                HeaderRowDifferentialStyleRecord = DifferentialStyleRecordHelper.GetDifferentialStyleRecord(c, c.GetOptionalInteger("headerRowDxfId"));
                DataDifferentialStyleRecord = DifferentialStyleRecordHelper.GetDifferentialStyleRecord(c, c.GetOptionalInteger("dataDxfId"));
                TotalsRowDiffertialStyleRecord = DifferentialStyleRecordHelper.GetDifferentialStyleRecord(c, c.GetOptionalInteger("totalsRowDxfId"));
                headerRowCellStyle = GetStyle (ss, c.GetOptionalString("headerRowCellStyle"));
                dataCellStyle = GetStyle (ss, c.GetOptionalString("dataCellStyle"));
                totalsRowCellStyle = GetStyle (ss, c.GetOptionalString("totalsRowCellStyle"));

                CalculatedFormula = c.ReadOptionalElementAs<TableFormula>("calculatedColumnFormula");
                TotalsRowFormula = c.ReadOptionalElementAs<TableFormula>("totalsRowFormula");
                XmlProperties = c.ReadOptionalElement<XmlColumnProperties>("xmlColumnPr");
                Extension = c.ReadOptionalElement<ExtensionElement>("extLst");
            }
        }

        private string GetStyle(StyleSheet ss, string name)
        {
            if (name == null)
                return null;
            return name;
        }


        void IOfficeXmlSerializable.WriteXml(WriteContext context)
        {
            using (WriteContext c = context.Write("tableColumn"))
            {
                c.SetUIntegerAsInteger("id", Id);
                c.SetOptionalString("uniqueName", UniqueName);
                c.SetString("name", Name);
                c.SetOptionalEnum("totalsRowFunction", TotalsRowFunction, TotalsRowFunction.None);
                c.SetOptionalString("totalsRowLabel", TotalsRowLabel);
                c.SetOptionalUIntegerAsInteger("queryTableFieldId", queryTableFieldId);
                DifferentialStyleRecordHelper.SetDifferentialStyleRecord(c, HeaderRowDifferentialStyleRecord, "headerRowDxfId");
                DifferentialStyleRecordHelper.SetDifferentialStyleRecord(c, DataDifferentialStyleRecord, "dataDxfId");
                DifferentialStyleRecordHelper.SetDifferentialStyleRecord(c, TotalsRowDiffertialStyleRecord, "totalsRowDxfId");
                SetStyle(c, headerRowCellStyle, "headerRowCellStyle");
                SetStyle(c, dataCellStyle, "dataCellStyle");
                SetStyle(c, totalsRowCellStyle, "totalsRowCellStyle");

                c.WriteOptionalElementAs(CalculatedFormula, "calculatedColumnFormula");
                c.WriteOptionalElementAs(TotalsRowFormula, "totalsRowFormula");
                c.WriteOptionalElement(XmlProperties);
                c.WriteOptionalElement(Extension);
            }
        }

        private ExtensionElement _extension;

        public ExtensionElement Extension
        {
            get { return _extension; }
            set { _extension = value; }
        }

        private void SetStyle(WriteContext c, string style, string name)
        {
            if (style == null)
                return;
            c.SetString(name, style);
        }


        #endregion
        private TableFormula _calculatedColumnFormula;

        public TableFormula CalculatedFormula
        {
            get { return _calculatedColumnFormula; }
            set { _calculatedColumnFormula = value; }
        }
        private TableFormula totalsRowFormula;

        public TableFormula TotalsRowFormula
        {
            get { return totalsRowFormula; }
            set { totalsRowFormula = value; }
        }
        private XmlColumnProperties xmlColumnPr;

        public XmlColumnProperties XmlProperties
        {
            get { return xmlColumnPr; }
            set { xmlColumnPr = value; }
        }
        private int id;

        public int Id
        {
            get { return id; }
            internal set { id = value; }
        }
        private string _name;

        public string Name
        {
            get { return _name; }
            internal set { _name = value; }
        }
        private string uniqueName;

        public string UniqueName
        {
            get { return uniqueName; }
            set { uniqueName = value; }
        }
        private TotalsRowFunction totalsRowFunction;

        public TotalsRowFunction TotalsRowFunction
        {
            get { return totalsRowFunction; }
            set { totalsRowFunction = value; }
        }
        private string totalsRowLabel;

        public string TotalsRowLabel
        {
            get { return totalsRowLabel; }
            set { totalsRowLabel = value; }
        }
        private int? queryTableFieldId;

        public int? QueryTableFieldId
        {
            get { return queryTableFieldId; }
            set { queryTableFieldId = value; }
        }

        private DifferentialStyleRecord _headerRowDifferentialStyleRecord;

        internal DifferentialStyleRecord HeaderRowDifferentialStyleRecord
        {
            get { return _headerRowDifferentialStyleRecord; }
            set { _headerRowDifferentialStyleRecord = value; }
        }
	
        private DifferentialStyleRecord _dataDxfId;

        internal DifferentialStyleRecord DataDifferentialStyleRecord
        {
            get { return _dataDxfId; }
            set { _dataDxfId = value; }
        }
        private DifferentialStyleRecord totalsRowDxfId;

        internal DifferentialStyleRecord TotalsRowDiffertialStyleRecord
        {
            get { return totalsRowDxfId; }
            set { totalsRowDxfId = value; }
        }
        private string headerRowCellStyle;

        public string HeaderRowCellStyle
        {
            get { return headerRowCellStyle; }
            set { headerRowCellStyle = value; }
        }
        private string dataCellStyle;

        public string DataCellStyle
        {
            get { return dataCellStyle; }
            set { dataCellStyle = value; }
        }
        private string totalsRowCellStyle;

        public string TotalsRowCellStyle
        {
            get { return totalsRowCellStyle; }
            set { totalsRowCellStyle = value; }
        }
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
    }
}
